package jflowmap.views.heatmap;

import java.awt.Color;
import java.awt.Shape;
import java.awt.geom.Point2D;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import jflowmap.model.geo.MapProjection;
import jflowmap.model.graph.Graph;
import jflowmap.model.graph.Node;
import jflowmap.model.map.GeoMap;
import jflowmap.model.ontology.Dimension;
import jflowmap.util.Lasso;
import jflowmap.util.map.PGeoMap;
import jflowmap.views.MapBackgroundImage;

import com.google.common.collect.Lists;

import edu.umd.cs.piccolo.PCamera;
import edu.umd.cs.piccolo.event.PBasicInputEventHandler;
import edu.umd.cs.piccolo.event.PInputEvent;

public class MapLayer extends AbstractLayer {
	
	private final PGeoMap geoMap;
	private final VisualHeatMap visualHeatMap;
	private final List<Centroid> centroids = Lists.newLinkedList();
	private final MapProjection projection;
	private final Lasso lasso;
	
	public MapLayer(VisualHeatMap visualHeatMap, double relativeWidth, MapBackgroundImage mapImage,
			MapProjection proj, GeoMap areaMap) {
		super(relativeWidth);
		
		this.visualHeatMap = visualHeatMap;	
		this.projection = proj;
		this.geoMap = new PGeoMap(visualHeatMap.getMapColorScheme(), areaMap, proj);
		this.addChild(geoMap);		
		
		getCamera().addPropertyChangeListener(PCamera.PROPERTY_VIEW_TRANSFORM, new PropertyChangeListener() {
			@Override
			public void propertyChange(PropertyChangeEvent event) {
				for (Centroid centroid: centroids) {
					centroid.updatePosition(getCamera());
				}
			}
		});
		
		lasso = new Lasso(getCamera(), Color.RED) {
			@Override
			public void selectionMade(Shape shape) {
				MapLayer.this.visualHeatMap.getHeatMapView().getView().getSelection("standard").empty("TARGET");
				
				final List<Dimension> dimensions = Lists.newArrayList();
				
				for (Centroid centroid: centroids) {
					if (shape.contains(centroid.getPoint())) {
						dimensions.add(centroid.getNode().getDimension());
					}
				}

				MapLayer.this.visualHeatMap.getHeatMapView().getView().getSelection("standard")
				.select("TARGET", dimensions);
			}
		};
		
		getCamera().addInputEventListener(lasso);
		
		createCentroids();
	}

	private void createCentroids() {
		final Graph graph = visualHeatMap.getGraph();
				
		for (final Node target: graph.getTargetNodes()) {
			
			final Point2D pos = projection.project(target.getLongitude(), target.getLatitude());
			final Centroid centroid = new Centroid(target, visualHeatMap, pos);
			
			getCamera().addChild(centroid);
			centroid.updatePosition(getCamera());
			
			centroids.add(centroid);
			
			centroid.addInputEventListener(new PBasicInputEventHandler() {
				@Override
				public void mouseClicked(PInputEvent event) {
					if (event.getClickCount() == 1) {
						visualHeatMap.getHeatMapView().getView()
						.getSelection("standard").toggle("TARGET", target.getDimension());
						
						//visualHeatMap.update();
					}
				}
			});
		}
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}
}
